<template>
  <div class="app-container">
    <el-table
      v-loading="loading"
      :data="logisticsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="订单号" align="center" prop="psOrderId" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.logistics_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="创建"
        align="center"
        prop="createTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更改用户" align="center" prop="userId" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.status == 0"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handlePackageDone(scope.row)"
            v-hasPermi="['supply:logistics:edit']"
            >点击打包完成</el-button
          >

          <el-button
            v-if="scope.row.status == 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleSending(scope.row)"
            v-hasPermi="['supply:logistics:remove']"
            >点击配送中</el-button
          >

          <el-button
            v-if="scope.row.status == 2"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleSendDone(scope.row)"
            v-hasPermi="['supply:logistics:remove']"
            >点击送达</el-button
          >

          <el-button
            v-if="scope.row.status == 3"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleSigned(scope.row)"
            v-hasPermi="['supply:logistics:remove']"
            >点击签收</el-button
          >

          <el-button
            v-if="scope.row.status == 4"
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['supply:logistics:remove']"
            >完成</el-button>

          <el-button
          v-if="scope.row.status != -1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            v-hasPermi="['supply:logistics:remove']"
            @click="handleCancleOrder(scope.row)"
            >取消订单</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { listLogistics, getLogistics, } from "@/api/supply/logistics";
import request from '@/utils/request'

export default {
  name: "Logistics",
  dicts: ['logistics_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物流表格数据
      logisticsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        psOrderId: null,
        status: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        psOrderId: [
          { required: true, message: "订单号不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        userId: [
          { required: true, message: "更改用户不能为空", trigger: "blur" }
        ]
      }
    };
  },
   async created() {
     await this.getLastInfo();
  },
  methods: {
    /** 查询物流列表 */
    getList() {
      this.loading = true;
      listLogistics(this.queryParams).then(response => {
        this.logisticsList = response.rows;
        this.total = response.total;
        this.loading = false;
        console.log(response);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        psOrderId: null,
        status: null,
        createTime: null,
        userId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物流";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getLogistics(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改物流";
      });
    },
    async getLastInfo(ids){
        let response=await request({
            url: '/supply/logistics/lasted',
            method: 'post'
        })
        this.logisticsList = response.rows;
        this.total = response.total;
        console.log(response);
        this.loading = false;
    },
    async handleCancleOrder(row){
        let res=await request({
            url: '/supply/psorders/cancel/'+row.psOrderId,
            method: 'put',
        })
        if(res.code==200){
          this.loading = true;
          await this.getLastInfo();
          this.loading = false;

        }else{
            console.log("报错了")
        }
    }
    ,
    async handlePackageDone(row){
        let res=await request({
            url: '/supply/logistics/package/done?orderId='+row.psOrderId,
            method: 'post',
        })
        if(res.code==200){
            let item = this.logisticsList.find(item => item.id == row.id);
            if (item) {
                Object.assign(item, this.getLastInfo({ ids: [row.id] }));
            }

        }else{
            console.log("报错了")
        }
    },
    async handleSending(row){
        let res=await request({
            url: '/supply/logistics/sender/ing?orderId='+row.psOrderId,
            method: 'post',
        })
        if(res.code==200){
            let item = this.logisticsList.find(item => item.id == row.id);
            if (item) {
                Object.assign(item, this.getLastInfo({ ids: [row.id] }));
            }
        }else{
            console.log("报错了")
        }
    },
    async handleSendDone(row){
        let res=await request({
            url: '/supply/logistics/sender/done?orderId='+row.psOrderId,
            method: 'post',
        })
        if(res.code==200){
            let item = this.logisticsList.find(item => item.id == row.id);
            if (item) {
                Object.assign(item, this.getLastInfo({ ids: [row.id] }));
            }
            console.log("报错了")
        }
    },
    async handleSigned(row){
        let res=await request({
            url: '/supply/logistics/sign/done?orderId='+row.psOrderId,
            method: 'post',
        })
        if(res.code==200){
            let item = this.logisticsList.find(item => item.id == row.id);
            if (item) {
                Object.assign(item, this.getLastInfo({ ids: [row.id] }));
            }
        }else{
            console.log("报错了")
        }
    },
  }
};
</script>
